package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.domain.BusStatementItem;
import cn.wolfcode.business.domain.vo.StatementItemVO;
import cn.wolfcode.business.mapper.BusStatementItemMapper;
import cn.wolfcode.business.service.BusStatementItemService;
import cn.wolfcode.business.service.IBusAppointmentService;
import cn.wolfcode.business.service.IBusStatementService;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BusStatementItemServiceImpl implements BusStatementItemService {
    @Autowired
    private BusStatementItemMapper busStatementItemMapper;
    @Autowired
    private IBusStatementService busStatementService;
    @Autowired
    private IBusAppointmentService busAppointmentService;

    @Override
    @Transactional
    public int save(StatementItemVO vo) {
        if (vo == null||vo.getStatementItemList()==null|| vo.getStatementItemList().isEmpty()||
        vo.getDiscountPrice()==null) {
            throw new RuntimeException("非法参数");
        }
        Long statementId = vo.getStatementItemList().get(0).getStatementId();
        BusStatement busStatement = busStatementService.selectBusStatementById(statementId);
        if (busStatement == null||busStatement.getStatus()==null) {
            throw new RuntimeException("非法参数");
        }
        if (BusStatement.STATUS_PAID.equals(busStatement.getStatus())) {
            throw new RuntimeException("必须是消费中的才可以保存！");
        }
        busStatementItemMapper.deleteByStatementId(statementId);
        //总价格
        BigDecimal totalAmount = new BigDecimal(0);
        //总数量
        BigDecimal totalQuality = new BigDecimal(0);
        //折扣价
        BigDecimal disCount = vo.getDiscountPrice();
        for (BusStatementItem item : vo.getStatementItemList()) {
            totalAmount = totalAmount.add(item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity())));
            totalQuality = totalQuality.add(new BigDecimal(item.getItemQuantity()));
        }
        if (totalAmount.compareTo(new BigDecimal(0))<0 && disCount.compareTo(new BigDecimal(0))<0 &&
            totalAmount.compareTo(disCount)<0){
            throw new RuntimeException("非法参数");
        }
        if (totalQuality.compareTo(new BigDecimal(0))<=0){
            throw new RuntimeException("非法参数");
        }
        busStatementService.updateItem(statementId,totalAmount,totalQuality,disCount);
        return  busStatementItemMapper.insertItem(vo.getStatementItemList());
    }

    @Override
    public List<BusStatementItem> list(Long statementId) {
        return busStatementItemMapper.selectByStatementId(statementId);
    }

    @Override
    public int pay(Long statementId) {
        if (statementId==null){
            throw new RuntimeException("非法参数");
        }
        BusStatement busStatement = busStatementService.selectBusStatementById(statementId);
        if (busStatement==null||busStatement.getStatus()==null){
            throw new RuntimeException("非法参数");
        }
        if (!BusStatement.STATUS_CONSUME.equals(busStatement.getStatus())){
            throw new RuntimeException("只有在消费中在能支付");
        }
        Long payId = SecurityUtils.getUserId();
        if(busStatement.getAppointmentId()!=null){
            busAppointmentService.updateStatus(BusAppointment.STATUS_PAID,busStatement.getAppointmentId());
        }
        return busStatementService.updateBusPay(statementId,payId,BusStatement.STATUS_PAID);
    }
}
